﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using UnityEngine;
public class DragObjectY : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler
{
    public float maxY;
    public float minY;
    private float posY;
    public bool isDrag = false;
    public float dragStartPosY;
    public VoidEvent startDragEvent;
    public VoidEvent endDragEvent;

    public RectTransform target;

    RectTransform cacheTarget
    {
        get
        {
            if (target == null)
            {
                target = GetComponent<RectTransform>();
            }
            return target;
        }
    }


    public void OnDrag(PointerEventData eventData)
    {
        posY = eventData.position.y;
        Debug.Log("posY : " + eventData.position.y);
        if (posY < minY)
        {
            //OnStartDragEvent();
            posY = minY;
        }
        else if (posY > maxY)
        {
            //OnStartDragEvent();
            posY = maxY;
        }
        target.position = new Vector3(target.position.x, posY, target.position.z);
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        isDrag = true;
        OnStartDragEvent();
        dragStartPosY = target.position.y;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        isDrag = false;
        if (null != endDragEvent)
            endDragEvent.Invoke();
    }

    private void OnStartDragEvent()
    {
        if (null != startDragEvent)
            startDragEvent.Invoke();
    }


}
